//
//  HELMaterialExamVC.m
//  sanda
//
//  Created by 陈武琦 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELMaterialExamVC.h"
#import "HELMaterialCell.h"
#import "HELPrepareForExamVC.h"
#import "HELSuperTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"

static NSString *cellID = @"HELMaterialCellID";
@interface HELMaterialExamVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
@property(nonatomic, assign)HELExamType type;
@property(nonatomic, copy)NSMutableArray *dataArray;
@end


@implementation HELMaterialExamVC


- (instancetype)initWithExamType:(HELExamType)type {
    if (self = [super init]) {
        _type = type;//：{type=3} 参数说明：3代表正式考试，2代表模拟考试，1代表资料测试
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor blueColor];
//    [self titleView:self.title];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
    [titleLabel setAdjustsFontSizeToFitWidth:YES];
    [titleLabel setText:self.title];
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.translucent = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"HELMaterialCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    weakSelf_define;
    // 添加下拉刷新头部控件
    [self.tableView wsj_HeaderRefreshBlock:^{
        //请求加载数据
        [weakSelf requestData];
    }];

    [self requestData];

}

- (void)requestData {
    
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    self.tableView.loading = YES;
    NSDictionary *param = @{@"type":@(self.type), @"userId":userInfo.userId};

    defineWeakSelf;
    [[HELNetworkManager sharedMg] POST:helMyExamUrl parameters:param success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dict in responseObject) {
                HELExamModel *model = [HELExamModel mj_objectWithKeyValues:dict];
                [arr addObject:model];
            }
            weakSelf.dataArray = [arr copy];
            [weakSelf.tableView reloadData];
        }
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }
        [weakSelf.tableView.header endRefreshing];
    } failure:^(NSString *err) {
        cwqHudRequestError(err);
        [weakSelf.tableView.header endRefreshing];
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }

    }];
}


- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 40-64) style:UITableViewStylePlain];
        _tableView.rowHeight =UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kAppBg;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView =[UIView new];
        
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}


#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//     HELExamModel *model = self.dataArray[indexPath.row];
//    CGFloat height = [tableView fd_heightForCellWithIdentifier:cellID configuration:^(UITableViewCell *cell) {
//        [cell setValue:model forKey:@"model"];
//    }];
//    return height;
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELExamModel *model = self.dataArray[indexPath.row];
    HELMaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HELExamModel *model = self.dataArray[indexPath.row];
    HELPrepareForExamVC *prepareVC = [[HELPrepareForExamVC alloc] initWithExamId:model.id];
    [self.navigationController pushViewController:prepareVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
